﻿#region Copyright & License
//
// Copyright 2010 sd&c Hofmann 
// 
// mailto:info@sdch.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.UserCode;
using Microsoft.Office.Server.Diagnostics;
using SDCH.SharePoint.Log4Sandbox.UserCode;
using SDCH.SharePoint.Log4Sandbox.UserCode.ProxyOperationArgs;
using log4net.Repository;
using log4net;
using log4net.Config;
using System.IO;
using log4net.Core;

namespace SDCH.SharePoint.Log4Sandbox.TrustedProxy
{
   public class GetLoggerOperation :SPProxyOperation
    {
        public override object Execute(SPProxyOperationArgs args)
        {
            try
            {
                RequestLoggerArgs requestArgs = (RequestLoggerArgs)args;
                ILog log = SandBoxLogManager.GetLogger(requestArgs.SiteID, requestArgs.Logger);
                return CreateLogContext(log, requestArgs);
            }
            catch (Exception ex)
            {
                PortalLog.DebugLogString(PortalLogLevel.Unexpected, "[{0}] {1}", ex.Message, ex.StackTrace);
                return false;
            }
        }

        private LogContext CreateLogContext(ILog pLog, RequestLoggerArgs pRequestArgs)
        {
            LogContext logContext = new LogContext();
            logContext.Logger = pRequestArgs.Logger;
            logContext.Repository = pRequestArgs.SiteID.ToString();

            logContext.IsInfoEnabled = pLog.IsInfoEnabled;
            logContext.IsDebugEnabled = pLog.IsDebugEnabled;
            logContext.IsErrorEnabled = pLog.IsErrorEnabled;
            logContext.IsFatalEnabled = pLog.IsFatalEnabled;
            logContext.IsWarnEnabled = pLog.IsWarnEnabled;

            return logContext;
        }
    }
}
